#include "resourcehandler.hpp"

#include "guichan/image.hpp"
#include "guichan/imagefont.hpp"
#include "guichan/font.hpp"

namespace nmc
{
	ResourceHandler::ResourceHandler()
	{

	}
	
	ResourceHandler::~ResourceHandler()
	{
		std::map<std::string, Image*>::iterator imagesIterator;

		for (imagesIterator = mImages.begin(); imagesIterator != mImages.end(); imagesIterator++)
		{
			delete (*imagesIterator).second;
		}

		std::map<std::string, gcn::Font*>::iterator fontsIterator;

		for (fontsIterator = mFonts.begin(); fontsIterator != mFonts.end(); fontsIterator++)
		{
			delete (*fontsIterator).second;
		}
	}

	Image* ResourceHandler::getImage(const std::string& filename)
	{
		if (mImages.find(filename) == mImages.end())
		{
			Image* image = (Image*)gcn::Image::load(filename);	    
			mImages[filename] = image;
		}

		return mImages[filename];
	}

	gcn::Font* ResourceHandler::getFont(const std::string& filename,
										unsigned char glyphsFrom,
										unsigned char glyphsTo)
	{
		if (mFonts.find(filename) == mFonts.end())
		{
			gcn::Font* font = new gcn::ImageFont(filename, glyphsFrom, glyphsTo);  
			mFonts[filename] = font;
		}

		return mFonts[filename];
	}
}